/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LearningSystemViewer.java
 *
 * Created on Feb 22, 2011, 5:12:24 PM
 */

package wekinator;

import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import wekinator.util.FileChooserWithExtension;

/**
 *
 * @author fiebrink
 */
public class LearningSystemViewer extends javax.swing.JPanel {

    /** Creates new form LearningSystemViewer */
    public LearningSystemViewer() {
        initComponents();
    }

    public LearningSystemViewer(LearningSystem ls) throws ParseException {
       initComponents();
        int numParams = ls.getNumParams();
        final SimpleDataset d = ls.getDataset();
        labelNumParams.setText(ls.getNumParams() + " parameters:");
        if (d != null) {

            final int numFeats = d.getNumFeatures();
            labelNumFeats.setText(numFeats + " features:");
            AbstractListModel m = new AbstractListModel() {

                public int getSize() {
                    return numFeats;
                }

                public Object getElementAt(int i) {
                   return d.getFeatureName(i);

                }
            };
            listFeatures.setModel(m);
            int numInstances = d.getNumDatapoints();
          /*  double dtime = d.getTimestamp(numInstances -1);

            String ds = "" + (int)dtime;

            while (ds.length() < 9) {
                ds = "0" + ds;
            } 
            Date date = SimpleDataset.dateFormat.parse(ds); */
            SimpleDateFormat prettyDateFormat = new SimpleDateFormat("MM/dd/yyyy 'at' HH:mm:ss");
            labelSummary.setText("Dataset contains " + numInstances + " examples.");
            //took out Modified " + prettyDateFormat.format(date)


        } else {
            labelNumFeats.setText("No dataset, no features");
            listFeatures.removeAll();
            labelSummary.setText("");
        }

        panelParams.removeAll();
        LearningSystemParameterViewer[] vs = new LearningSystemParameterViewer[numParams];
        for (int i = 0; i < numParams; i++) {
            vs[i] = new LearningSystemParameterViewer(ls, i);
            panelParams.add(vs[i]);
        }
        panelParams.setPreferredSize(new Dimension(400,  263 * numParams));
        panelParams.repaint();

        this.repaint();

    }

    public static void main(String[] args) throws Exception {
        JFrame frame = new JFrame();
        //frame.setPreferredSize(new Dimension(478, 532));
        frame.setSize(new Dimension(478, 532));
                FileChooserWithExtension fc = new FileChooserWithExtension("wlsys", "Learning system", null, new File("/Users/fiebrink/work/projects/wekinator/project/mySavedSettings/learningSystems"), false);
        int returnVal = fc.showOpenDialog(frame);
        File file = null;
        if (returnVal == FileChooserWithExtension.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            LearningSystem ls = LearningSystem.readFromFile(file);
            LearningSystemViewer panel = new LearningSystemViewer(ls);

                frame.add(panel);
                frame.setVisible(true);
        }
                
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        panelParams = new javax.swing.JPanel();
        learningSystemParameterViewer1 = new wekinator.LearningSystemParameterViewer();
        jScrollPane2 = new javax.swing.JScrollPane();
        listFeatures = new javax.swing.JList();
        labelNumFeats = new javax.swing.JLabel();
        labelNumParams = new javax.swing.JLabel();
        labelSummary = new javax.swing.JLabel();

        panelParams.setLayout(new javax.swing.BoxLayout(panelParams, javax.swing.BoxLayout.Y_AXIS));
        panelParams.add(learningSystemParameterViewer1);

        jScrollPane1.setViewportView(panelParams);

        listFeatures.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(listFeatures);

        labelNumFeats.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        labelNumFeats.setText("N Features:");

        labelNumParams.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        labelNumParams.setText("N Parameters:");

        labelSummary.setText("Dataset contains N instances. Last modified 8/1/2010 at 8:35:00 PM.");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(labelSummary, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(labelNumFeats)
                            .add(labelNumParams))
                        .add(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(labelNumFeats)
                .add(4, 4, 4)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(labelNumParams)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(labelSummary, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 26, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel labelNumFeats;
    private javax.swing.JLabel labelNumParams;
    private javax.swing.JLabel labelSummary;
    private wekinator.LearningSystemParameterViewer learningSystemParameterViewer1;
    private javax.swing.JList listFeatures;
    private javax.swing.JPanel panelParams;
    // End of variables declaration//GEN-END:variables

}
